package com.example.deploysinggle.test1.common.util;

import com.example.deploysinggle.test1.common.util.dto.Functions;
import lombok.extern.slf4j.Slf4j;

/**
 * @author Administrator
 * @version 1.0
 * @description: TODO
 * @date 2024/11/20 15:28
 */
@Slf4j
public class RetryUtil {
    public static void work(Functions.VoidFunction function, int retryTimes){
        for (int i = 0; i < retryTimes; i++) {
            try {
                FunctionUtil.work(function);
                return;
            } catch (Exception e) {
               log.error("execute failed, current {}, times: {} ",i,retryTimes,e);
            }
        }
        throw new RuntimeException("execute method failed after retry");

    }
    public static <R> R work(Functions.FunctionR <R>function,int retryTimes){
        for (int i = 0; i < retryTimes; i++) {
            try {
                return FunctionUtil.work(function);
            } catch (Exception e) {
                log.error("execute failed, current {}, times: {} ",i,retryTimes,e);
            }
        }
        throw new RuntimeException("execute method failed after retry");
    }
    public static <T1,R> R work(Functions.Function<T1,R> function,T1 t1,int retryTimes){
        for (int i = 0; i < retryTimes; i++) {
            try {
               return FunctionUtil.work(function,t1);

            } catch (Exception e) {
                log.error("execute failed, current {}, times: {} ",i,retryTimes,e);
            }
        }
        throw new RuntimeException("execute method failed after retry");
    }

    public static <T1,T2,R> R work(Functions.BiFunction<T1,T2,R> function,T1 t1,T2 t2,int retryTimes){
        for (int i = 0; i < retryTimes; i++) {
            try {
                return FunctionUtil.work(function, t1, t2);
            } catch (Exception e) {
                log.error("execute failed, current {}, times: {} ",i,retryTimes,e);
            }
        }
        throw new RuntimeException("execute method failed after retry");
    }
    public static <T1,T2,T3,R> R work(Functions.TriFunction<T1,T2,T3,R> function,T1 t1,T2 t2,T3 t3,int retryTimes){
        for (int i = 0; i < retryTimes; i++) {
            try {
                return FunctionUtil.work(function, t1, t2, t3);
            } catch (Exception e) {
                log.error("execute failed, current {}, times: {} ",i,retryTimes,e);
            }
        }
        throw new RuntimeException("execute method failed after retry");
    }

    public static <T1,T2,T3,T4,R> R work(Functions.FourFunction<T1,T2,T3,T4,R> function,T1 t1,T2 t2,T3 t3,T4 t4,int retryTimes){
        for (int i = 0; i < retryTimes; i++) {
            try {
                return FunctionUtil.work(function, t1, t2, t3, t4);
            } catch (Exception e) {
                log.error("execute failed, current {}, times: {} ",i,retryTimes,e);
            }
        }
        throw new RuntimeException("execute method failed after retry");
    }
    public static <T1,T2,T3,T4,T5,R> R work(Functions.FiverFunction<T1,T2,T3,T4,T5,R> function,T1 t1,T2 t2,T3 t3,T4 t4,T5 t5,int retryTimes){
        for (int i = 0; i < retryTimes; i++) {
            try {
                return FunctionUtil.work(function, t1, t2, t3, t4, t5);
            } catch (Exception e) {
                log.error("execute failed, current {}, times: {} ",i,retryTimes,e);
            }
        }
        throw new RuntimeException("execute method failed after retry");
    }

    public static <T1,T2,T3,T4,T5,T6,R> R work(Functions.SixrFunction<T1,T2,T3,T4,T5,T6,R> function,T1 t1,T2 t2,T3 t3,T4 t4,T5 t5,T6 t6,int retryTimes){
        for (int i = 0; i < retryTimes; i++) {
            try {
                return FunctionUtil.work(function, t1, t2, t3, t4, t5, t6);
            } catch (Exception e) {
                log.error("execute failed, current {}, times: {} ",i,retryTimes,e);
            }
        }
        throw new RuntimeException("execute method failed after retry");
    }

    public static <T1,T2,T3,T4,T5,T6,T7,R> R work(Functions.SevenFunction<T1,T2,T3,T4,T5,T6,T7,R> function,T1 t1,T2 t2,T3 t3,T4 t4,T5 t5,T6 t6,T7 t7,int retryTimes){
        for (int i = 0; i < retryTimes; i++) {
            try {
                return FunctionUtil.work(function, t1, t2, t3, t4, t5, t6, t7);
            } catch (Exception e) {
                log.error("execute failed, current {}, times: {} ",i,retryTimes,e);
            }
        }
        throw new RuntimeException("execute method failed after retry");    }

    public static <T1,T2,T3,T4,T5,T6,T7,T8,R> R work(Functions.EightrFunction<T1,T2,T3,T4,T5,T6,T7,T8,R> function,T1 t1,T2 t2,T3 t3,T4 t4,T5 t5,T6 t6,T7 t7,T8 t8,int retryTimes){
        for (int i = 0; i < retryTimes; i++) {
            try {
                return FunctionUtil.work(function, t1, t2, t3, t4, t5, t6, t7, t8);
            } catch (Exception e) {
                log.error("execute failed, current {}, times: {} ",i,retryTimes,e);
            }
        }
        throw new RuntimeException("execute method failed after retry");    }

    public static <T1,T2,T3,T4,T5,T6,T7,T8,T9,R> R work(Functions.NinerFunction<T1,T2,T3,T4,T5,T6,T7,T8,T9,R> function,T1 t1,T2 t2,T3 t3,T4 t4,T5 t5,T6 t6,T7 t7,T8 t8,T9 t9,int retryTimes){
        for (int i = 0; i < retryTimes; i++) {
            try {
                return FunctionUtil.work(function, t1, t2, t3, t4, t5, t6, t7, t8, t9);
            } catch (Exception e) {
                log.error("execute failed, current {}, times: {} ",i,retryTimes,e);
            }
        }
        throw new RuntimeException("execute method failed after retry");    }
}
